<template lang="pug">
div
  g-h.top(j-c="flex-end")
    //- g-h.tab(j-c="center")
      div.tab-item.tab-item-start(
          @click="curTab = ''"
          :class="{tabIsSelected: curTab === ''}"
          ) 非消耗
      div.tab-item.tab-item-end(
          @click="curTab = 'cost'"
          :class="{tabIsSelected: curTab === 'cost'}"
          ) 消耗
    div.button(@click="save") 整入整出保存

  KSlotPair.one(k="领料单位" slotWidth="12rem")
    DropDownList.dropdown-list(
        :list="group.list"
        :curValue="group.vTitle"
        @select="select"
        :style="{zIndex: '30'}"
        )

  //- Cascade(
      :filterData="filterData"
      v-show="curTab !== 'cost'"
      )

</template>

<script>
import api from "$api"
import util from "$util"

const PLANT = 2290

export default {
  props: {
    // 是否显示整入整出
    isVisible: {
      type: Boolean,
      default: false,
    },
    // 父页面的表格数据
    bodyList: {
      type: Array,
      required: true,
    },
    // 父页面单号
    ReceiveId: {
      type: String | null,
      required: true,
    },
    // 厂家编号
    PullSupplierNum: {
      type: Number | null,
      required: true,
    },
  },
  data() {
    let filterData = [
      {
        k: "StorgelocationCode",
        kTitle: "目的实体仓库",
        async getOptionList() {
          try {
            let data = await api.OrderAllInGetReal({
              Plant: PLANT,
            })
            let newData = []
            for (let item of data) {
              if (item.ItemValue !== "") {
                newData.push({
                  id: item.ItemValue,
                  name: item.ItemText,
                })
              }
            }
            return newData
          } catch(err) {
            this.$root.error(err)
          }
        },
      },
      {
        k: "WmNo",
        kTitle: "目的逻辑仓库",
        async getOptionList(req) {
          req.Plant = PLANT 
          try {
            let data = await api.OrderAllInGetLogical(req)
            let newData = []
            for (let item of data) {
              if (item.ItemValue !== "") {
                newData.push({
                  id: item.ItemValue,
                  name: item.ItemText,
                })
              }
            }
            return newData
          } catch(err) {
            this.$root.error(err)
          }
        },
      },
      {
        k: "ZoneNo",
        kTitle: "目的存储区",
        async getOptionList(req) {
          req.Plant = PLANT 
          try {
            let data = await api.OrderAllInGetZone(req)
            let newData = []
            for (let item of data) {
              if (item.ItemValue !== "") {
                newData.push({
                  id: item.ItemValue,
                  name: item.ItemText,
                })
              }
            }
            return newData
          } catch(err) {
            this.$root.error(err)
          }
        },
      },
      {
        k: "Dloc",
        kTitle: "目的库位",
        meta: {
          PullSupplierNum: "",
        },
        async getOptionList(req) {
          req.Plant = PLANT 
          req.PullSupplierNum = this.meta.PullSupplierNum
          try {
            let data = await api.OrderAllInGetLocation(req)
            let newData = []
            for (let item of data) {
              if (item.ItemValue !== "") {
                newData.push({
                  id: item.ItemValue,
                  name: item.ItemText,
                })
              }
            }
            return newData
          } catch(err) {
            this.$root.error(err)
          }
        },
      },
    ]

    return {
      group: {
        v: "",
        vTitle: "",
        list: [],
      },

      loading: {
        save: false,
      },

      curTab: "cost",

      filterData,
    }
  },
  watch: {
    PullSupplierNum(v) {
      this.filterData[this.filterData.length - 1].meta.PullSupplierNum = v
    },
    isVisible(v) {
      if (v && this.group.list.length === 0) {
        this.getDept()
      }
    },
  },
  methods: {
    select(option) {
      this.group.v = option.v
      this.group.vTitle = option.vTitle
    },

    async save() {
      if (this.loading.save) {
        return
      }

      let param = {
        ReceiveId: this.ReceiveId,
        infoList: this.bodyList,
      }
      /* if (this.curTab === "cost") {
        param["Donetype"] = 1
      } else {
        param["Donetype"] = 2
        for (let block of this.filterData) {
          param[block.k] = block.v
        }
      } */
      if (!this.group.v) {
        util.Alert("请选择领料单位")
        return
      }
      param["Dept"] = this.group.v

      this.loading.save = true
      try {
        await api.OrderAllInSave(param)
        util.Toast("整入整出保存成功")
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
        this.loading.save = false
      }
    },

    // 获取领料单位下拉数据
    async getDept() {
      try {
        let data = await api.OrderAllInGetDept()
        let newData = new Array(data.length)
        let i = 0
        for (let item of data) {
          newData[i] = {
            id: item.DetailCode,
            name: item.DetailValue,
          }
          i++
        }
        this.group.list = newData
      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
      }
    },
  },
}
</script>

<style lang="stylus" scoped>

.c-root
  height: 60vh

.top
  margin: 1rem

.tab
  &-item
    padding: 0 0.5rem
    width: 5rem
    text-align: center
    &-start
      border: 1px solid grey500
      border-radius: 1rem 0 0 1rem
    &-end
      border-top: 1px solid grey500
      border-right: 1px solid grey500
      border-bottom: 1px solid grey500
      border-radius: 0 1rem 1rem 0

.tabIsSelected
  background: blue500

.button
  padding: 0 0.5rem
  background: teal500
  border-radius: 1rem
  &:active
    background: teal300

.one
  margin: 1rem 0

.dropdown-list
  width: 100%

</style>
